<template>
  <view>
    <view class="address-choose" v-if="JSON.stringify(address) === '{}'">
      <button type="primary" size="mini" @click="chooseAddress">请选择收货地址</button>
    </view>
    <view class="address" v-else @click="chooseAddress">
      <view class="address-top">
        <view class="receiver">收货人：{{ address.userName }}</view>
        <view class="phone">
          <view class="phoneNum">电话：{{ address.telNumber }}</view>
          <uni-icons type="right" size="18"></uni-icons>
        </view>
      </view>
      <view class="address-bottom">收货地址：{{ longAddress }}</view>
    </view>
    <image src="../../static/cart_border@2x.png" class="address_border"></image>
  </view>
</template>

<script>
  import { mapState, mapGetters, mapMutations } from 'vuex'
  export default {
    name:"my-address",
    computed: {
      ...mapState('m_user', ['address']),
      ...mapGetters('m_user', ['longAddress'])
    },
    data() {
      return {
      };
    },
    methods: {
      ...mapMutations('m_user', ['updateAddress']),
      async chooseAddress(){
        const [err, success] = await uni.chooseAddress().catch(err => err)
        if(err === null && success.errMsg === 'chooseAddress:ok'){
          this.updateAddress(success)
        } 
      }
    }
  }
</script>

<style lang="scss">
  .address-choose {
    display: flex;
    height: 180rpx;
    justify-content: space-between;
    align-items: center;
  }
  .address {
    padding: 20rpx 10rpx;
    font-size: 26rpx;
    .address-top {
      margin-bottom: 20rpx;
      display: flex;
      justify-content: space-between;
      .phone {
        display: flex;
        justify-content: space-between;
      }
    }
  }
  .address_border {
    display: block;
    width: 100%;
    height: 10rpx;
  }
</style>